<template>
	<view class="pending-records-container">
		<view class="page-header">
			<text class="page-title">待确认记录</text>
		</view>
		
		<!-- 加载状态 -->
		<view class="loading-section" v-if="loading">
			<text class="loading-text">加载中...</text>
		</view>
		
		<!-- 空状态 -->
		<view class="empty-section" v-else-if="!groupedMatches || groupedMatches.length === 0">
			<text class="empty-icon">📋</text>
			<text class="empty-text">暂无待确认记录</text>
			<text class="empty-desc">当有新的执裁记录需要确认时，会在这里显示</text>
		</view>
		
		<!-- 场次列表 -->
		<view class="matches-list" v-else>
			<view class="match-card" v-for="match in groupedMatches" :key="match.matchKey" @click="openMatchEvaluation(match)">
				<!-- 比赛基本信息 -->
				<view class="match-header">
					<view class="match-title">{{match.match_title}}</view>
					<view class="match-status status-pending">
						<text>待评价确认</text>
					</view>
				</view>
				
				<view class="match-info">
					<view class="info-row">
						<text class="info-label">比赛时间：</text>
						<text class="info-value">{{formatDateTime(match.match_date)}}</text>
					</view>
					<view class="info-row">
						<text class="info-label">比赛地点：</text>
						<text class="info-value">{{match.match_venue}}</text>
					</view>
					<view class="info-row">
						<text class="info-label">对阵双方：</text>
						<text class="info-value">{{match.home_team}} VS {{match.away_team}}</text>
					</view>
					<view class="info-row">
						<text class="info-label">比赛级别：</text>
						<text class="info-value">{{match.match_level}}</text>
					</view>
				</view>
				
				<!-- 裁判员列表 -->
				<view class="referees-section">
					<view class="section-title">执裁裁判员 ({{match.records.length}}人)</view>
					<view class="referees-list">
						<view class="referee-item" v-for="record in match.records" :key="record._id">
							<view class="referee-info">
								<text class="referee-name">{{record.referee_name}}</text>
								<text class="referee-position" :class="{
									'position-main': record.position === '主裁判',
									'position-assistant': record.position === '助理裁判' || record.position === '边裁判',
									'position-fourth': record.position === '第四官员',
									'position-supervisor': record.position === '裁判监督'
								}">{{record.position}}</text>
							</view>
							<view class="referee-status">
								<text class="status-text">待评价</text>
							</view>
						</view>
					</view>
				</view>
				
				<!-- 操作按钮 -->
				<view class="match-actions">
					<view class="action-info">
						<text class="action-text">点击进入评价页面</text>
					</view>
					<view class="action-arrow">
						<text>></text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			loading: true,
			records: [],
			groupedMatches: [],
			currentUserId: ''
		}
	},
	
	onLoad() {
		this.getCurrentUserId()
		this.loadPendingRecords()
	},
	
	onShow() {
		// 页面显示时刷新数据
		this.loadPendingRecords()
	},
	
	methods: {
		// 获取当前用户ID
		getCurrentUserId() {
			const userInfo = uni.getStorageSync('userInfo')
			if (userInfo) {
				try {
					const userData = JSON.parse(userInfo)
					this.currentUserId = userData._id || userData.uid
				} catch (e) {
					console.error('解析用户信息失败:', e)
				}
			}
		},
		
		// 加载待确认记录
		async loadPendingRecords() {
			this.loading = true
			try {
				const refereeRecordService = uniCloud.importObject('referee-record-service')
				const result = await refereeRecordService.getPendingRecords()
				
				if (result.code === 0) {
					this.records = result.data || []
					this.groupRecordsByMatch()
				} else {
					this.handleError(result.message || '获取待确认记录失败')
				}
			} catch (error) {
				this.handleError('网络错误: ' + error.message)
			} finally {
				this.loading = false
			}
		},
		
		// 按场次分组记录
		groupRecordsByMatch() {
			const matchMap = new Map()
			
			this.records.forEach(record => {
				// 使用比赛ID + 日期 + 地点作为分组键，确保同一场比赛的记录归为一组
				const matchKey = `${record.match_id}_${this.formatDate(record.match_date)}_${record.match_venue}`
				
				if (!matchMap.has(matchKey)) {
					matchMap.set(matchKey, {
						matchKey: matchKey,
						match_id: record.match_id,
						match_title: record.match_title,
						match_date: record.match_date,
						match_venue: record.match_venue,
						match_level: record.match_level,
						home_team: record.home_team,
						away_team: record.away_team,
						records: []
					})
				}
				
				matchMap.get(matchKey).records.push(record)
			})
			
			// 转换为数组并按时间排序
			this.groupedMatches = Array.from(matchMap.values()).sort((a, b) => {
				return new Date(a.match_date) - new Date(b.match_date)
			})
			
			console.log('按场次分组后的数据:', this.groupedMatches)
		},
		
		// 打开场次评价页面
		openMatchEvaluation(match) {
			// 验证数据完整性
			if (!match.records || match.records.length === 0) {
				uni.showToast({
					title: '该场次没有待确认记录',
					icon: 'none'
				})
				return
			}
			
			// 将场次数据存储到本地，然后跳转到评价页面
			const matchData = {
				...match,
				recordIds: match.records.map(r => r._id)
			}
			
			console.log('跳转到评价页面，数据:', matchData)
			
			uni.setStorageSync('currentMatchEvaluation', JSON.stringify(matchData))
			
			uni.navigateTo({
				url: `/packageMy/my/match-evaluation?matchKey=${match.matchKey}`
			})
		},
		
		// 格式化日期
		formatDate(dateStr) {
			if (!dateStr) return ''
			const date = new Date(dateStr)
			return date.getFullYear() + '-' + 
				   String(date.getMonth() + 1).padStart(2, '0') + '-' + 
				   String(date.getDate()).padStart(2, '0')
		},
		
		// 格式化日期时间
		formatDateTime(dateStr) {
			if (!dateStr) return ''
			const date = new Date(dateStr)
			return date.getFullYear() + '-' + 
				   String(date.getMonth() + 1).padStart(2, '0') + '-' + 
				   String(date.getDate()).padStart(2, '0') + ' ' +
				   String(date.getHours()).padStart(2, '0') + ':' + 
				   String(date.getMinutes()).padStart(2, '0')
		},
		
		// 错误处理
		handleError(message) {
			uni.showToast({
				title: message,
				icon: 'none'
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.pending-records-container {
	min-height: 100vh;
	background-color: #f5f5f5;
}

.page-header {
	background-color: #fff;
	padding: 30rpx 20rpx;
	text-align: center;
	border-bottom: 2rpx solid #f0f0f0;
}

.page-title {
	font-size: 36rpx;
	font-weight: bold;
	color: #333;
}

.loading-section, .empty-section {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding: 100rpx 40rpx;
}

.loading-text {
	font-size: 28rpx;
	color: #666;
}

.empty-icon {
	font-size: 120rpx;
	margin-bottom: 30rpx;
	opacity: 0.5;
}

.empty-text {
	font-size: 32rpx;
	color: #666;
	margin-bottom: 20rpx;
}

.empty-desc {
	font-size: 26rpx;
	color: #999;
	text-align: center;
	line-height: 1.5;
}

.matches-list {
	padding: 20rpx;
}

.match-card {
	background-color: #fff;
	border-radius: 20rpx;
	padding: 30rpx;
	margin-bottom: 20rpx;
	box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
}

.match-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 20rpx;
}

.match-title {
	font-size: 34rpx;
	font-weight: bold;
	color: #333;
	flex: 1;
}

.match-status {
	padding: 8rpx 16rpx;
	border-radius: 20rpx;
	font-size: 24rpx;
}

.status-pending {
	background-color: #fff3cd;
	color: #856404;
}

.match-info {
	margin-bottom: 25rpx;
}

.info-row {
	display: flex;
	margin-bottom: 12rpx;
}

.info-label {
	font-size: 26rpx;
	color: #666;
	width: 140rpx;
	flex-shrink: 0;
}

.info-value {
	font-size: 26rpx;
	color: #333;
	flex: 1;
}

.referees-section {
	margin-bottom: 25rpx;
}

.section-title {
	font-size: 28rpx;
	font-weight: bold;
	color: #333;
	margin-bottom: 15rpx;
}

.referees-list {
	background-color: #f8f9fa;
	border-radius: 12rpx;
	padding: 15rpx;
}

.referee-item {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 12rpx 0;
	border-bottom: 1rpx solid #e9ecef;
}

.referee-item:last-child {
	border-bottom: none;
}

.referee-info {
	display: flex;
	align-items: center;
	flex: 1;
}

.referee-name {
	font-size: 28rpx;
	color: #333;
	margin-right: 15rpx;
}

.referee-position {
	font-size: 22rpx;
	padding: 4rpx 8rpx;
	border-radius: 10rpx;
	color: #fff;
}

.position-main {
	background-color: #ff6b6b;
}

.position-assistant {
	background-color: #4ecdc4;
}

.position-fourth {
	background-color: #45b7d1;
}

.position-supervisor {
	background-color: #96ceb4;
}

.position-default {
	background-color: #999;
}

.referee-status {
	flex-shrink: 0;
}

.status-text {
	font-size: 24rpx;
	color: #856404;
}

.match-actions {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding-top: 15rpx;
	border-top: 1rpx solid #f0f0f0;
}

.action-info {
	flex: 1;
}

.action-text {
	font-size: 26rpx;
	color: #007aff;
}

.action-arrow {
	color: #999;
	font-size: 28rpx;
}
</style> 